package com.cal.utms.dao.dealing;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.DealInstrumentMap;
import com.cal.utms.entity.Instrument;
import com.cal.utms.util.HibernateUtil;

public class DealInstrumentMapDao {

	public Object add(DealInstrumentMap dim) {
		Object obj;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		System.out.println("Adding new DealInsMap: " + dim.getDeal());
		try {
			tx = session.beginTransaction();
			obj = session.save(dim);
			tx.commit();
			System.out.println("Deal Added: " + dim.getDeal());
		} catch (Exception ex) {
			tx.rollback();
			obj = new Object();
			System.out.println("Exception in save Deal " + dim.getDeal() + " : " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;
	}

	public void update(DealInstrumentMap dim) {
		System.out.println("Updating the dim");
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(dim);
			tx.commit();
			System.out.println("DealInstrumentMap updated");
		} catch (Exception ex) {
			tx.rollback();
			System.out.println("Exception in Updating the DealInstrumentMap. ID: " + dim.getId() + " : " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<DealInstrumentMap> getByCode(Instrument instrument) {
		Session session = HibernateUtil.getSession().openSession();
		List<DealInstrumentMap> dimList = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("select c from DealInstrumentMap c where c.instrument = :ins");
			query.setParameter("ins", instrument);
			dimList = query.list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID DealInstrumentMap " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return dimList;
	}

	@SuppressWarnings("unchecked")
	public List<DealInstrumentMap> getPurchasesForIns(int insId) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealInstrumentMap> res = null;
		try {
			tx = session.beginTransaction();
			Query query = session
					.createQuery("select c from DealInstrumentMap c where c.instrument.id = :insId and c.deal.dealType.code='PURCH' and c.deal.status='Confirmed' ORDER BY c.date DESC");
			query.setParameter("insId", insId);
			res = query.list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID DealInstrumentMap " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	public DealInstrumentMap getPurchForIns(int dimId) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealInstrumentMap> res = null;
		DealInstrumentMap dim = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("select c from DealInstrumentMap c where c.id = :dimId ORDER BY c.date DESC");
			// Query query =
			// session.createQuery("select c from DealInstrumentMap c where c.instrument.id = :dimId ORDER BY c.date DESC");
			query.setParameter("dimId", dimId);
			res = query.list();
			if (res.size() > 0)
				dim = res.get(0);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID DealInstrumentMap " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return dim;
	}

	@SuppressWarnings("unchecked")
	public DealInstrumentMap getByDeal(int dealId) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<DealInstrumentMap> res = null;
		DealInstrumentMap dim = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("select c from DealInstrumentMap c where c.deal.id=:dimId");
			query.setParameter("dimId", dealId);
			res = query.list();
			if (res.size() > 0)
				dim = res.get(0);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID DealInstrumentMap " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return dim;
	}
}
